<template>
  <div style="padding: 10px">
    <div style="margin: 10px 0">
      <el-popconfirm title="确认离校吗?" @confirm="addLeave" v-if="user.role === 0">
        <template #reference>
          <el-button type="danger" size="small" icon="el-icon-delete">确认离校
          </el-button>
        </template>
      </el-popconfirm>
      <el-button type="" @click="deleteLeave"  v-if="user.role === 0">取消离校</el-button>


    </div>
    <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%">

      <el-table-column prop="stuId"
                       label="学号"
                       sortable/>
      <el-table-column prop="floor"
                       label="楼号"/>


      <el-table-column prop="dormId"
                       label="宿舍号"/>

      <el-table-column prop="Ltime"
                       label="离校时间"
                       sortable/>

    </el-table>
    <div>
      <!--      分页-->
      <el-pagination
          v-model:currentPage="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      >
      </el-pagination>

    </div>
  </div>
</template>

<script>

import request from "../utils/request";
import {ElMessage} from "element-plus";
// import {ElMessage} from 'element-plus'

export default {
  name: 'Home',
  components: {},
  data() {
    return {
      currentPage: 1,
      total: 0,
      tableData: [],
      pageSize: 10,
      form: {
        stuId: '',
        floor: 0,
        dormId: 0,
        Ltime: ''
      },
    }
  },
  created() {
    let userJson = sessionStorage.getItem("user") || "{}"
    console.log(userJson);
    this.form.stuId = JSON.parse(userJson).userId;
    this.form.floor = JSON.parse(userJson).floor;
    this.form.dormId = JSON.parse(userJson).dormId;
    //将session取出的json数据转化为对象类型
    this.user = JSON.parse(userJson)
    console.log(this.form);
    this.load();
  },
  methods: {
    load() {
      request.get("/leave",
          //多个参数get方式需要添加params属性
          {
            params: {
              page: this.currentPage,
              rows: this.pageSize,
            }
          }).then(res => {
        console.log(res);
        if (res.code === '0') {
          this.tableData = res.data.rows;
          this.total = res.data.total;
        }

      })
    },
    addLeave() {
      request.post("/leave", this.form).then(res => {
        console.log(res);
        if (res.code === '0') {
          ElMessage({
            message: '新增成功!',
            type: 'success',
          })
        } else {
          ElMessage({
            message: res.msg,
            type: 'error'
          })
        }
        this.load();
      });
    },
    deleteLeave() {
        request.delete("/leave/" + this.form.stuId).then(res => {
          if (res.code === '0') {
            ElMessage({
              message: '取消离校成功!',
              type: 'success',
            })
          } else {
            ElMessage({
              message: '取消离校失败!',
              type: 'error'
            })
          }
          this.load();
        });
    },




    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },

  }
}
</script>
